﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ChoViet.CMS.Components
{
    internal class FormsAuthenticationService : IFormsAuthenticationService
    {
        public void SignIn(string userName)
        {
            SignOut();
            System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false);
            System.Security.Principal.GenericIdentity identity = new System.Security.Principal.GenericIdentity(userName);
            System.Security.Principal.GenericPrincipal user = new System.Security.Principal.GenericPrincipal(identity, null);
            System.Web.HttpContext.Current.User = user;
        }

        public void SignOut()
        {
            System.Web.Security.FormsAuthentication.SignOut();
        }

        public string GetCurrentUserName()
        {
            try
            {
                System.Security.Principal.IIdentity identity = System.Web.HttpContext.Current.User.Identity;
                if (identity == null)
                    return string.Empty;

                return identity.IsAuthenticated ? identity.Name : string.Empty;
            }
            catch { }

            return string.Empty;
        }
    }
}
